package com.villaday.idf.activity;

import android.content.Intent;
import android.os.Handler;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.R;
import com.villaday.idf.adapter.BournSearchForMoreAdapter;
import com.villaday.idf.base.AbsRecyclerViewAdapter;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.custom.LoadingDialog;
import com.villaday.idf.custom.DividerItemDecoration.RVItemDecoration;
import com.villaday.idf.model.BournSearchhistory;
import com.villaday.idf.model.Resources_filtersEntity;
import com.villaday.idf.model.Search_filtersEntity;
import com.villaday.idf.model.V;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.DbBSHUtils;
import com.villaday.idf.util.DbHUtils;
import com.villaday.idf.util.DbVUtils;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.PhoneUtil;
import com.villaday.idf.util.SharedPreferencesUtil;
import com.villaday.idf.util.ToastUtil;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnEditorAction;
import me.bakumon.statuslayoutmanager.library.OnStatusChildClickListener;
import me.bakumon.statuslayoutmanager.library.StatusLayoutManager;

import static com.baidu.location.g.j.e;

/*
 * 搜索目的地
 * */
public class SearchBournActivity<T> extends BaseActivity implements OkHttpUtil.OnDataListener, TextWatcher, AbsRecyclerViewAdapter.OnItemClickListener {


    @BindView(R.id.toolbar_searchBox)
    public AutoCompleteTextView autoTextView;
    @BindView(R.id.img_delete)
    ImageView imgDelete;
    @BindView(R.id.fl_history)
    TagFlowLayout flHistory;
    @BindView(R.id.ll_delete)
    LinearLayout llDelete;
    @BindView(R.id.img_hot)
    ImageView imgHot;
    @BindView(R.id.fl_hot)
    TagFlowLayout flHot;
    @BindView(R.id.ll_hot)
    LinearLayout llHot;
    @BindView(R.id.img_view)
    ImageView imgView;
    @BindView(R.id.fl_view)
    TagFlowLayout flView;
    @BindView(R.id.ll_view)
    LinearLayout llView;
    @BindView(R.id.img_area)
    ImageView imgArea;
    @BindView(R.id.fl_area)
    TagFlowLayout flArea;
    @BindView(R.id.ll_area)
    LinearLayout llArea;
    @BindView(R.id.img_shop)
    ImageView imgShop;
    @BindView(R.id.fl_shop)
    TagFlowLayout flShop;
    @BindView(R.id.ll_shop)
    LinearLayout llShop;
    @BindView(R.id.img_airport)
    ImageView imgAirport;
    @BindView(R.id.fl_airport)
    TagFlowLayout flAirport;
    @BindView(R.id.ll_airport)
    LinearLayout llAirport;
    @BindView(R.id.img_subway)
    ImageView imgSubway;
    @BindView(R.id.fl_subway)
    TagFlowLayout flSubway;
    @BindView(R.id.ll_subway)
    LinearLayout llSubway;
    @BindView(R.id.img_school)
    ImageView imgSchool;
    @BindView(R.id.fl_school)
    TagFlowLayout flSchool;
    @BindView(R.id.ll_school)
    LinearLayout llSchool;
    @BindView(R.id.img_hothouse)
    ImageView imgHothouse;
    @BindView(R.id.fl_hothouse)
    TagFlowLayout flHothouse;
    @BindView(R.id.ll_hothouse)
    LinearLayout llHothouse;
    //搜索时涉及控件
    @BindView(R.id.ll_head)
    LinearLayout llHead;


    @BindView(R.id.rv_search)
    RecyclerView rvSearch;
    TagFlowLayout flSearch;
    /*@BindView(R.id.nsv)
    NestedScrollView nsv;*/
    TextView tvFangyuan;

    @BindView(R.id.img_delete_search)
    ImageView imgDeleteSearch;

    @BindView(R.id.ll)
    LinearLayout ll;


    private String cityId;
    private LoadingDialog loadingDialog;

    private Gson gson = new GsonBuilder().create();
    private Search_filtersEntity search_filtersEntity;
    private Resources_filtersEntity resources_filtersEntity;
    private DbHUtils hDb;//房源
    private DbVUtils vDb;//目的地
    private BournSearchForMoreAdapter bournSearchAdapter;
    private TagAdapter<T> tagSearchAdapter;
    private List<com.villaday.idf.model.H> hs;
    private DbBSHUtils bSHDb;
    private StatusLayoutManager statusLayoutManager;
    private Handler handler;
    private Runnable runnable;
    private LinearLayout llSearch;
    private ImageView imgSearch;


    @Override
    protected int getViewResId() {
        return R.layout.activity_search_bourn;
    }

    @OnEditorAction(R.id.toolbar_searchBox)
    public boolean onEditAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
            //ToastUtil.initToast(this, "hhhh");
            PhoneUtil.hideKeyboard(v);
            CharSequence text = v.getText();
            if (!TextUtils.isEmpty(text)) {
                /*bSHDb.insertOrReplace(text.toString(), System.currentTimeMillis());
                List<BournSearchhistory> bournSearchhistories = bSHDb.queryDownAllOrderByDate();
                setData(flHistory, llDelete, (List<T>) bournSearchhistories, false);*/
                Intent intent = new Intent(this, SearchBournForHouseActivity.class);
                intent.putExtra("text",text.toString());
                startActivity(intent);
            }


            return true;
        }
        return false;
    }

    @Override
    protected void loadData() {
        loadingDialog = LoadingDialog.newInstance("加载中");
        loadingDialog.show(getFragmentManager(), "loading");
        OkHttpUtil.getJSON(this, String.format(Constant.URL.search_filters, cityId), this);
        if (SharedPreferencesUtil.getHomeResTimeIsOver(this)) {
            OkHttpUtil.getJSON(this, Constant.URL.resources_filters, this);
        } else {
            initHouseRes(SharedPreferencesUtil.getHomeRes(this));
        }


    }

    @Override
    protected void init() {

        bournSearchAdapter = new BournSearchForMoreAdapter(this,rvSearch);
        View inflate = View.inflate(this, R.layout.bourn_search_head, null);

        flSearch = inflate.findViewById(R.id.fl_search);

        llSearch = inflate.findViewById(R.id.ll_search);

        tvFangyuan = inflate.findViewById(R.id.tv_fangyuan);



                imgSearch = inflate.findViewById(R.id.img_search);
        imgSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgSearch.setSelected(!imgSearch.isSelected());
                if (imgSearch.isSelected()) {
                    setViewHight(flSearch, true);
                } else {
                    setViewHight(flSearch, false);

                }
            }
        });

        statusLayoutManager = setupDefaultStatusLayoutManager(ll, new OnStatusChildClickListener() {
            @Override
            public void onEmptyChildClick(View view) {

            }

            @Override
            public void onErrorChildClick(View view) {
                loadingDialog = LoadingDialog.newInstance("加载中");
                loadingDialog.show(getFragmentManager(), "loading");

                loadData();
            }

            @Override
            public void onCustomerChildClick(View view) {

            }
        });
        hDb = DbHUtils.getInstance();
        vDb = DbVUtils.getInstance();
        bSHDb = DbBSHUtils.getInstance();
        cityId = getIntent().getStringExtra("cityId");


        autoTextView.addTextChangedListener(this);
        rvSearch.setLayoutManager(new LinearLayoutManager(this));
        rvSearch.setNestedScrollingEnabled(false);


        bournSearchAdapter.addHeaderView(inflate);
        rvSearch.setAdapter(bournSearchAdapter);

        rvSearch.addItemDecoration(new RVItemDecoration(LinearLayout.VERTICAL, PhoneUtil.dp2px(this, 1), getResources().getColor(R.color.colorBg), PhoneUtil.dp2px(this, 0)));
        bournSearchAdapter.setOnItemClickListener(this);
       /* View inflate = View.inflate(getContext(), R.layout.recycle_head, null);
        homeListSaleAdapter.addHeaderView(inflate);*/
        bournSearchAdapter.setEmptyView(R.layout.empty_search_tips_white);
       // bournSearchAdapter.setEmptyTips(R.id.tv_emptyTips, "抱歉，未能搜索到相关内容，请按关键字搜索");
         bournSearchAdapter.setEmptyTips(R.id.tv_emptyTips, "");

        rvSearch.setNestedScrollingEnabled(false);

        List<BournSearchhistory> bournSearchhistories = bSHDb.queryDownAllOrderByDate();
        setData(flHistory, llDelete, (List<T>) bournSearchhistories, true);
    }


    @Override
    public void onResponse(String url, String json) {
        if (statusLayoutManager != null) {

            statusLayoutManager.showSuccessLayout();
        }
        if (String.format(Constant.URL.search_filters, cityId).equals(url)) {
            if (!SharedPreferencesUtil.getHomeResTimeIsOver(this)) {
                dismissLoading();
            }
            LogeUtil.e("search_filters", json);
            search_filtersEntity = gson.fromJson(json, Search_filtersEntity.class);
            if (search_filtersEntity != null && search_filtersEntity.getBody() != null && "200".equals(search_filtersEntity.getCode())) {
                initView();

            }

        } else if (Constant.URL.resources_filters.equals(url)) {
            LogeUtil.e("resources_filters", json);
            dismissLoading();
            SharedPreferencesUtil.saveHomeRes(this, System.currentTimeMillis(),json);
            initHouseRes(json);

        }
    }

    private void initHouseRes(String json) {
        resources_filtersEntity = gson.fromJson(json, Resources_filtersEntity.class);
        if (resources_filtersEntity != null && resources_filtersEntity.getBody() != null && "200".equals(resources_filtersEntity.getCode())) {
            hDb.deleteAll();
            vDb.deleteAll();

            hDb.saveAll(resources_filtersEntity.getBody().getH());
            vDb.saveAll(resources_filtersEntity.getBody().getV());
            setViewHight(flSearch, false);

        }
    }

    private void initView() {
        final Search_filtersEntity.BodyEntity body = search_filtersEntity.getBody();
        //当地热门

        setData(flHot, llHot, (List<T>) body.getVillages(), true);
        //观光景点
        setData(flView, llView, (List<T>) body.getScenic_spots(), true);

        //行政区
        setData(flArea, llArea, (List<T>) body.getAreas(), true);
        //商圈
        setData(flShop, llShop, (List<T>) body.getBusiness(), true);
        //机场 车站
        setData(flAirport, llAirport, (List<T>) body.getStation(), true);
        //地铁线路
        setData(flSubway, llSubway, (List<T>) body.getSubway_lines(), true);
        //高校
        setData(flSchool, llSchool, (List<T>) body.getColleges(), true);
        //热门房源
        setData(flHothouse, llHothouse, (List<T>) body.getHouses(), false);

    }

    private void setData(final TagFlowLayout fl, LinearLayout ll, final List<T> object, boolean isRetract) {
        if (object == null || object.size() == 0) {
            ll.setVisibility(View.GONE);

        } else {
            ll.setVisibility(View.VISIBLE);

        }
        if (isRetract) {
            setViewHight(fl, false);
        }

        if (object != null) {
            // ll.setVisibility(View.VISIBLE);
            TagAdapter<T> tagAdapter = new TagAdapter<T>(object) {
                @Override
                public View getView(FlowLayout parent, int position, T s) {
                    TextView tv = (TextView) getLayoutInflater().inflate(R.layout.tv,
                            fl, false);
                    if (s instanceof Search_filtersEntity.BodyEntity.VillagesEntity) {
                        //当地热门

                        Search_filtersEntity.BodyEntity.VillagesEntity hot = (Search_filtersEntity.BodyEntity.VillagesEntity) s;
                        if (hot.getName().length() > 10) {
                            tv.setText(hot.getName().substring(0, 9) + "...");

                        } else {
                            tv.setText(hot.getName());

                        }
                    } else if (s instanceof Search_filtersEntity.BodyEntity.ScenicSpotsEntity) {
                        //观光景点
                        Search_filtersEntity.BodyEntity.ScenicSpotsEntity hot = (Search_filtersEntity.BodyEntity.ScenicSpotsEntity) s;
                        if (hot.getName().length() > 10) {
                            tv.setText(hot.getName().substring(0, 9) + "...");

                        } else {
                            tv.setText(hot.getName());

                        }
                    } else if (s instanceof Search_filtersEntity.BodyEntity.AreasEntity) {
                        //行政区
                        Search_filtersEntity.BodyEntity.AreasEntity hot = (Search_filtersEntity.BodyEntity.AreasEntity) s;
                        if (hot.getName().length() > 10) {
                            tv.setText(hot.getName().substring(0, 9) + "...");

                        } else {
                            tv.setText(hot.getName());

                        }
                    } else if (s instanceof Search_filtersEntity.BodyEntity.BusinessEntity) {
                        //商圈
                        Search_filtersEntity.BodyEntity.BusinessEntity hot = (Search_filtersEntity.BodyEntity.BusinessEntity) s;
                        if (hot.getName().length() > 10) {
                            tv.setText(hot.getName().substring(0, 9) + "...");

                        } else {
                            tv.setText(hot.getName());

                        }
                    } else if (s instanceof Search_filtersEntity.BodyEntity.StationEntity) {
                        //机场车站
                        Search_filtersEntity.BodyEntity.StationEntity hot = (Search_filtersEntity.BodyEntity.StationEntity) s;
                        if (hot.getName().length() > 10) {
                            tv.setText(hot.getName().substring(0, 9) + "...");

                        } else {
                            tv.setText(hot.getName());

                        }
                    } else if (s instanceof Search_filtersEntity.BodyEntity.SubwayLinesEntity) {
                        //地铁
                        Search_filtersEntity.BodyEntity.SubwayLinesEntity hot = (Search_filtersEntity.BodyEntity.SubwayLinesEntity) s;
                        if (hot.getName().length() > 10) {
                            tv.setText(hot.getName().substring(0, 9) + "...");

                        } else {
                            tv.setText(hot.getName());

                        }
                    } else if (s instanceof Search_filtersEntity.BodyEntity.CollegesEntity) {
                        //高校
                        Search_filtersEntity.BodyEntity.CollegesEntity hot = (Search_filtersEntity.BodyEntity.CollegesEntity) s;
                        if (hot.getName().length() > 10) {
                            tv.setText(hot.getName().substring(0, 9) + "...");

                        } else {
                            tv.setText(hot.getName());

                        }
                    } else if (s instanceof Search_filtersEntity.BodyEntity.HousesEntity) {
                        //热门房源
                        Search_filtersEntity.BodyEntity.HousesEntity hot = (Search_filtersEntity.BodyEntity.HousesEntity) s;
                        if (hot.getName().length() > 10) {
                            tv.setText(hot.getName().substring(0, 9) + "...");

                        } else {
                            tv.setText(hot.getName());

                        }
                    } else if (object.get(0) instanceof V) {
                        //搜索
                        V hot = (V) s;

                        if (hot.getName().length() > 10) {
                            tv.setText(hot.getName().substring(0, 9) + "...");

                        } else {
                            tv.setText(hot.getName());

                        }
                    } else if (object.get(0) instanceof BournSearchhistory) {
                        //搜索
                        BournSearchhistory hot = (BournSearchhistory) s;

                        if (hot.getName().length() > 10) {
                            tv.setText(hot.getName().substring(0, 9) + "...");

                        } else {
                            tv.setText(hot.getName());

                        }
                    }


                    return tv;
                }
            };
            fl.setAdapter(tagAdapter);
            fl.setOnTagClickListener(new TagFlowLayout.OnTagClickListener()

            {
                @Override
                public boolean onTagClick(View view, int position, FlowLayout parent) {
                    if (!TextUtils.isEmpty(autoTextView.getText().toString())) {
                        bSHDb.insertOrReplace(autoTextView.getText().toString(), System.currentTimeMillis());
                        List<BournSearchhistory> bournSearchhistories = bSHDb.queryDownAllOrderByDate();
                        setData(flHistory, llDelete, (List<T>) bournSearchhistories, false);

                    }

                    if (object.get(0) instanceof Search_filtersEntity.BodyEntity.VillagesEntity) {
                        //当地热门
                        List<Search_filtersEntity.BodyEntity.VillagesEntity> hot = (List<Search_filtersEntity.BodyEntity.VillagesEntity>) object;
                        Intent intent = new Intent(SearchBournActivity.this, BourbonListActivity.class);
                        intent.putExtra("villageId", hot.get(position).getId());
                        intent.putExtra("city", "");
                        intent.putExtra("area", "");
                        startActivity(intent);
                    } else if (object.get(0) instanceof Search_filtersEntity.BodyEntity.ScenicSpotsEntity) {
                        //观光景点


                        List<Search_filtersEntity.BodyEntity.ScenicSpotsEntity> hot = (List<Search_filtersEntity.BodyEntity.ScenicSpotsEntity>) object;
                        Intent intent = new Intent(SearchBournActivity.this, SearchBournForHouseActivity.class);
                        intent.putExtra("scenicSpotId", hot.get(position).getId());
                        intent.putExtra("city", "");
                        intent.putExtra("area", "");
                        startActivity(intent);
                    } else if (object.get(0) instanceof Search_filtersEntity.BodyEntity.AreasEntity) {
                        //行政区

                        List<Search_filtersEntity.BodyEntity.AreasEntity> hot = (List<Search_filtersEntity.BodyEntity.AreasEntity>) object;
                        Intent intent = new Intent(SearchBournActivity.this, SearchBournForHouseActivity.class);
                        intent.putExtra("area", hot.get(position).getName());
                        intent.putExtra("city", "");

                        startActivity(intent);
                    } else if (object.get(0) instanceof Search_filtersEntity.BodyEntity.BusinessEntity) {
                        //商圈


                        List<Search_filtersEntity.BodyEntity.BusinessEntity> hot = (List<Search_filtersEntity.BodyEntity.BusinessEntity>) object;
                        Intent intent = new Intent(SearchBournActivity.this, SearchBournForHouseActivity.class);
                        intent.putExtra("businesId", hot.get(position).getId());
                        intent.putExtra("city", "");
                        intent.putExtra("area", "");
                        startActivity(intent);
                    } else if (object.get(0) instanceof Search_filtersEntity.BodyEntity.StationEntity) {
                        //机场车站


                        List<Search_filtersEntity.BodyEntity.StationEntity> hot = (List<Search_filtersEntity.BodyEntity.StationEntity>) object;
                        Intent intent = new Intent(SearchBournActivity.this, SearchBournForHouseActivity.class);
                        intent.putExtra("stationId", hot.get(position).getId());
                        intent.putExtra("city", "");
                        intent.putExtra("area", "");
                        startActivity(intent);
                    } else if (object.get(0) instanceof Search_filtersEntity.BodyEntity.SubwayLinesEntity) {
                        //地铁

                        List<Search_filtersEntity.BodyEntity.SubwayLinesEntity> hot = (List<Search_filtersEntity.BodyEntity.SubwayLinesEntity>) object;
                        Intent intent = new Intent(SearchBournActivity.this, SearchBournForHouseActivity.class);
                        intent.putExtra("subwayLineId", hot.get(position).getId());
                        intent.putExtra("city", "");
                        intent.putExtra("area", "");
                        startActivity(intent);
                    } else if (object.get(0) instanceof Search_filtersEntity.BodyEntity.CollegesEntity) {
                        //高校
                        List<Search_filtersEntity.BodyEntity.CollegesEntity> hot = (List<Search_filtersEntity.BodyEntity.CollegesEntity>) object;
                        Intent intent = new Intent(SearchBournActivity.this, SearchBournForHouseActivity.class);
                        intent.putExtra("collegeId", hot.get(position).getId());
                        intent.putExtra("city", "");
                        intent.putExtra("area", "");
                        startActivity(intent);
                    } else if (object.get(0) instanceof Search_filtersEntity.BodyEntity.HousesEntity) {
                        //热门房源
                        List<Search_filtersEntity.BodyEntity.HousesEntity> hot = (List<Search_filtersEntity.BodyEntity.HousesEntity>) object;
                        Intent intent = new Intent(SearchBournActivity.this, HouseDetailsActivityFor3.class);
                        intent.putExtra("type", 0);
                        intent.putExtra("houseId", hot.get(position).getId());
                        intent.putExtra("presellProduct", hot.get(position).getHouseType() != 0);

                        intent.putExtra("brokerAge", hot.get(position).getBrokerAge());
                        intent.putExtra("persellId", hot.get(position).getProductId());
                        startActivity(intent);
                    } else if (object.get(0) instanceof V) {//搜索
                        List<V> hot = (List<V>) object;
                        Intent intent = new Intent(SearchBournActivity.this, BourbonListActivity.class);
                        intent.putExtra("villageId", hot.get(position).getVID());
                        intent.putExtra("city", "");
                        intent.putExtra("area", "");
                        startActivity(intent);
                    } else if (object.get(0) instanceof BournSearchhistory) {//搜索
                        List<BournSearchhistory> hot = (List<BournSearchhistory>) object;
                        autoTextView.setText(hot.get(position).getName());
                    }

                    return true;
                }
            });
        } else {
            //ll.setVisibility(View.GONE);

        }


    }

    private void setDataForSearch(final TagFlowLayout fl, LinearLayout ll, final List<T> object, boolean isRetract) {

    }

    private void setViewHight(View view, boolean isMatch) {
        if (isMatch) {
            view.getLayoutParams().height = LinearLayout.MarginLayoutParams.WRAP_CONTENT;
        } else {
            view.getLayoutParams().height = PhoneUtil.dp2px(this, 55);

        }
        view.requestLayout();
    }

    @Override
    public void onFailure(String url, String error) {
        dismissLoading();
        if (statusLayoutManager != null) {

            statusLayoutManager.showErrorLayout();
        }
        ToastUtil.initToast(this,"网络错误");
    }

    private void dismissLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
        /*if (srl != null && srl.isRefreshing()) {
            srl.setRefreshing(false);
        }*/

    }


    @OnClick({R.id.img_delete_search,  R.id.tv_cancle, R.id.img_delete, R.id.img_hot, R.id.img_view, R.id.img_area, R.id.img_shop, R.id.img_airport, R.id.img_subway, R.id.img_school, R.id.img_hothouse})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_delete_search://搜索置空
                autoTextView.setText("");
                imgDeleteSearch.setVisibility(View.INVISIBLE);
                break;
            case R.id.tv_cancle:
                onBackPressed();
                break;
            case R.id.img_delete:
                bSHDb.deleteAll();
                List<BournSearchhistory> bournSearchhistories = bSHDb.queryDownAllOrderByDate();
                setData(flHistory, llDelete, (List<T>) bournSearchhistories, false);
                break;
            case R.id.img_search:
                /*imgSearch.setSelected(!imgSearch.isSelected());
                if (imgSearch.isSelected()) {
                    setViewHight(flSearch, true);
                } else {
                    setViewHight(flSearch, false);

                }*/


                break;
            case R.id.img_hot:
                imgHot.setSelected(!imgHot.isSelected());
                if (imgHot.isSelected()) {
                    setViewHight(flHot, true);
                } else {
                    setViewHight(flHot, false);

                }


                break;
            case R.id.img_view:
                imgView.setSelected(!imgView.isSelected());
                if (imgView.isSelected()) {
                    setViewHight(flView, true);
                } else {
                    setViewHight(flView, false);

                }
                break;
            case R.id.img_area:
                imgArea.setSelected(!imgArea.isSelected());
                if (imgArea.isSelected()) {
                    setViewHight(flArea, true);
                } else {
                    setViewHight(flArea, false);

                }
                break;
            case R.id.img_shop:
                imgShop.setSelected(!imgShop.isSelected());
                if (imgShop.isSelected()) {
                    setViewHight(flShop, true);
                } else {
                    setViewHight(flShop, false);

                }
                break;
            case R.id.img_airport:
                imgAirport.setSelected(!imgAirport.isSelected());
                if (imgAirport.isSelected()) {
                    setViewHight(flAirport, true);
                } else {
                    setViewHight(flAirport, false);

                }
                break;
            case R.id.img_subway:
                imgSubway.setSelected(!imgSubway.isSelected());
                if (imgSubway.isSelected()) {
                    setViewHight(flSubway, true);
                } else {
                    setViewHight(flSubway, false);

                }
                break;
            case R.id.img_school:
                imgSchool.setSelected(!imgSchool.isSelected());
                if (imgSchool.isSelected()) {
                    setViewHight(flSchool, true);
                } else {
                    setViewHight(flSchool, false);

                }
                break;
            case R.id.img_hothouse:
                imgHothouse.setSelected(!imgHothouse.isSelected());
                if (imgHothouse.isSelected()) {
                    setViewHight(flHothouse, true);
                } else {
                    setViewHight(flHothouse, false);

                }
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(final CharSequence s, int start, int before, int count) {
       if (handler!=null&&runnable!=null){
           handler.removeCallbacks(runnable);
       }
        handler = new Handler();
        //当地热门
        runnable = new Runnable() {
            @Override
            public void run() {
                if (TextUtils.isEmpty(s)) {
                    llHead.setVisibility(View.VISIBLE);
                    llSearch.setVisibility(View.GONE);
                    rvSearch.setVisibility(View.GONE);
                    tvFangyuan.setVisibility(View.GONE);
                    imgDeleteSearch.setVisibility(View.INVISIBLE);

                } else {
                    llHead.setVisibility(View.GONE);
                    llSearch.setVisibility(View.VISIBLE);
                    rvSearch.setVisibility(View.VISIBLE);
                    imgDeleteSearch.setVisibility(View.VISIBLE);

                }
                if (resources_filtersEntity != null && resources_filtersEntity.getBody() != null) {
                    //当地热门
                    if (!TextUtils.isEmpty(s)) {
                        bournSearchAdapter.setSearchData(s.toString());
                        hs = hDb.queryWithSomethingAll(s.toString());
                        List<T> vs = (List<T>) vDb.queryWithSomethingAll(s.toString());
                        if (hs == null || hs.size() == 0) {
                            tvFangyuan.setVisibility(View.GONE);
                            if (vs.size() != 0) {
                                rvSearch.setVisibility(View.GONE);
                            }
                        } else {
                            tvFangyuan.setVisibility(View.VISIBLE);
                        }
                        if (TextUtils.isEmpty(s) || vs == null || vs.size() == 0) {
                            llSearch.setVisibility(View.GONE);
                        } else {
                            llSearch.setVisibility(View.VISIBLE);

                        }

                        bournSearchAdapter.setData(hs);
                        setData(flSearch, llSearch, vs, false);
                        /*
                        * List<com.villaday.idf.model.H> hs1=new ArrayList<>();

                        hs1.add(hs.get(0));
                        bournSearchAdapter.setData(hs1);

                        List<T> vs1 = (List<T>) vDb.queryWithSomethingAll(s.toString());
                        vs1.add(vs.get(0));
                        setData(flSearch, llSearch, vs1, false)
                        * */
                    }

                }
            }
        };
        handler.post(runnable);


    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void onItemClick(View v, int position) {
        if (hs.size() > position) {
            if (!TextUtils.isEmpty(autoTextView.getText().toString())) {
                bSHDb.insertOrReplace(autoTextView.getText().toString(), System.currentTimeMillis());
                List<BournSearchhistory> bournSearchhistories = bSHDb.queryDownAllOrderByDate();
                setData(flHistory, llDelete, (List<T>) bournSearchhistories, false);

            }

            Intent intent = new Intent(SearchBournActivity.this, HouseDetailsActivityFor3.class);
            intent.putExtra("type", 0);
            intent.putExtra("houseId", hs.get(position).getHId());
            intent.putExtra("presellProduct", false);

            intent.putExtra("brokerAge", hs.get(position).getCurrentPrice());
            intent.putExtra("persellId", hs.get(position).getHId());
            startActivity(intent);

        }


    }
}
